﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocios;

namespace INFTUR
{
    public partial class ManterUsuario : System.Web.UI.Page
    {
        public static bool inserir = false;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        
        public void Limpar()
        {
            //desabilitando campo

            tbID.Text = "";
            tbID.Enabled = false; 
            tbNome.Text = "";
            tbNome.Enabled = false;
            ddlTipo.SelectedValue = "0";
            ddlTipo.Enabled = false;
            tbEmail.Text = "";
            tbEmail.Enabled = false;
            tbSenha.Text = "";
            tbSenha.Enabled = false;
            lbErro.Text = "";

            //desabilitando botao
            btCancelar.Enabled = false;
            btSalvar.Enabled = false;
        }
        
        public void Novo()
        {
            //habilita campo
            
            tbNome.Enabled = true;
            tbID.Enabled = true;
            ddlTipo.Enabled = true;
            tbEmail.Enabled = true;
            tbSenha.Enabled = true;
            //habilita botao
            btCancelar.Enabled = true;
            btSalvar.Enabled = true;
        }

        protected void btNovo_Click(object sender, EventArgs e)
        {
            Limpar();
            Novo();

            inserir = true;
        }

        protected void btCancelar_Click(object sender, EventArgs e)
        {
            Limpar();
        }

        protected void btSalvar_Click(object sender, EventArgs e)
        {
            try
            {
            UsuarioBO cBO = new UsuarioBO();
            //Consertar Aqui!
            cBO.Nome = tbNome.Text;
            cBO.Tipo = Convert.ToInt32(ddlTipo.SelectedValue);
            cBO.Email = tbEmail.Text;
            cBO.Login = tbID.Text;
            cBO.Senha = tbSenha.Text;
            
            UsuarioBOMetodos cBOMetodos = new UsuarioBOMetodos();
            
            if (inserir)
                {
                    cBOMetodos.inserir(cBO);
                }
                else
                {
                    cBOMetodos.Atualiza(cBO);
                }
                    Limpar();
                    GridView1.DataBind();
            }
            catch(Exception ex) 
            {
                lbErro.Text = ex.Message;
            }
        }
     
        protected void btAtualizar_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)(lb).Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            string login = linha.Cells[0].Text;

            UsuarioBO cBO = new UsuarioBO();
            UsuarioBOMetodos cBOM = new UsuarioBOMetodos();

            cBO = cBOM.Consulta(login);
            Novo();

            //Consertar AQUI!
            ddlTipo.Text = cBO.Tipo.ToString();
            tbEmail.Text = cBO.Email;
            tbID.Text = cBO.Login;
            tbNome.Text = cBO.Nome;
            tbSenha.Text = cBO.Senha;

            inserir = false;
        }
 
        protected void TextBox7_TextChanged(object sender, EventArgs e)
        {

        }

        protected void lbRemover_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            DataControlFieldCell celula = (DataControlFieldCell)(lb).Parent;
            GridViewRow linha = (GridViewRow)celula.Parent;
            string login = linha.Cells[0].Text;

            UsuarioBO cBO = new UsuarioBO();
            UsuarioBOMetodos cBOM = new UsuarioBOMetodos();

            cBOM.Remove(login);

            GridView1.DataBind();
        }
    }
}
